package myBeans;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.SQLException;

import javax.naming.NamingException;
import model.NewsRepository;
//import org.apache.myfaces.custom.fileupload.UploadedFile;
import org.apache.commons.fileupload.*;

public class NewsInfo implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int newsId;
	private String newsTitle;
	private String newsDetail;
	private FileItem newsAttachFile;
	//private UploadedFile newsAttachFile;
	private Blob newsAttatchImage;
	private String newsAttatchContentType;
		
	public boolean isHaveAttatchment() {
		if(newsAttatchContentType != "")
			return true;
		else
			return false;
	}

	public String getNewsAttatchContentType() {
		return newsAttatchContentType;
	}

	public void setNewsAttatchContentType(String newsAttatchContentType) {
		this.newsAttatchContentType = newsAttatchContentType;
	}

	public int getNewsId() {
		return newsId;
	}

	public void setNewsId(int newsId) {
		this.newsId = newsId;
	}

	public Blob getNewsAttatchImage() {
		return newsAttatchImage;
	}

	public void setNewsAttatchImage(Blob blob) {
		this.newsAttatchImage = blob;
	}
	
	public FileItem getNewsAttachFile() {
		return newsAttachFile;
	}

	public String getNewsTitle() {
		return newsTitle;
	}
	public void setNewsTitle(String newsTitle) {
		this.newsTitle = newsTitle;
	}
	public String getNewsDetail() {
		return newsDetail;
	}
	public void setNewsDetail(String newsDetail) {
		this.newsDetail = newsDetail;
	}
	
	public void setNewsAttachFile(FileItem newsAttachFile) {
		this.newsAttachFile = newsAttachFile;
	}
	public String AddNews() throws IOException
	{
		 //String fileName = "Test"; //FilenameUtils.getName(newsAttachFile.getName());
		try {
			if(this.newsAttachFile == null)
				System.out.println("newsAttachFile: NULL");
			else
				System.out.println("newsAttachFile: " + this.newsAttachFile.getName());
			
			boolean addNewsSuccess = new NewsRepository().AddNews(this);
						
			if(addNewsSuccess)
				return "success";
			else
				return "failure";
			
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	       // FacesContext.getCurrentInstance().addMessage(null, 
	            //new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));

	        
		return "failure";
	}
	
}
